import type { FormSchema } from '#/adapter/form';
import type { VxeTableColumnOptions } from '#/adapter/vxe-table';

// 房态枚举
export enum RoomStatus {
  MAINTENANCE = 'maintenance', // 维修中
  RENTED = 'rented', // 已出租
  RESERVED = 'reserved', // 已预订
  VACANT = 'vacant', // 空置
}

// 朝向枚举
export enum Orientation {
  EAST = 'east', // 东
  NORTH = 'north', // 北
  NORTHEAST = 'northeast', // 东北
  NORTHWEST = 'northwest', // 西北
  SOUTH = 'south', // 南
  SOUTHEAST = 'southeast', // 东南
  SOUTHWEST = 'southwest', // 西南
  WEST = 'west', // 西
}

// 装修风格枚举
export enum DecorationStyle {
  DELUXE = 'deluxe', // 豪装
  LUXURY = 'luxury', // 精装
  MEDIUM = 'medium', // 中装
  SIMPLE = 'simple', // 简装
}

// 价格区间枚举
export enum PriceRange {
  HIGH = 'high', // 高价
  LOW = 'low', // 低价
  LUXURY = 'luxury', // 豪华
  MEDIUM = 'medium', // 中价
}

// 出租类型枚举
export enum RentType {
  SHARED = 'shared', // 合租
  WHOLE = 'whole', // 整租
}

// 房源信息接口
export interface RoomInfo {
  id: string;
  roomNumber: string;
  area: number;
  orientation: Orientation;
  facilities: string[];
  rent: number;
  deposit: number;
  images: string[];
  status: RoomStatus;
  decorationStyle: DecorationStyle;
  priceRange: PriceRange;
  rentType: RentType;
  tags: string[];
  description?: string;
  address?: string;
  floor?: number;
  building?: string;
  createdAt: string;
  updatedAt: string;
  iotDevices: IOTDevice[];
}

// IOT设备接口
export interface IOTDevice {
  id: string;
  name: string;
  type: string;
  status: string;
  lastUpdate: string;
  battery?: number;
  signal?: number;
}

// 表单Schema
export function useFormSchema(): FormSchema[] {
  return [
    {
      field: 'roomNumber',
      label: '房间号',
      component: 'Input',
      required: true,
      componentProps: {
        placeholder: '请输入房间号',
      },
    },
    {
      field: 'area',
      label: '面积(㎡)',
      component: 'InputNumber',
      required: true,
      componentProps: {
        min: 0,
        precision: 2,
        placeholder: '请输入面积',
        style: { width: '100%' },
      },
    },
    {
      field: 'orientation',
      label: '朝向',
      component: 'Select',
      componentProps: {
        placeholder: '选择朝向',
        options: [
          { label: '北', value: Orientation.NORTH },
          { label: '南', value: Orientation.SOUTH },
          { label: '东', value: Orientation.EAST },
          { label: '西', value: Orientation.WEST },
          { label: '东北', value: Orientation.NORTHEAST },
          { label: '西北', value: Orientation.NORTHWEST },
          { label: '东南', value: Orientation.SOUTHEAST },
          { label: '西南', value: Orientation.SOUTHWEST },
        ],
      },
    },
    {
      field: 'decorationStyle',
      label: '装修风格',
      component: 'Select',
      componentProps: {
        placeholder: '选择装修风格',
        options: [
          { label: '简装', value: DecorationStyle.SIMPLE },
          { label: '中装', value: DecorationStyle.MEDIUM },
          { label: '精装', value: DecorationStyle.LUXURY },
          { label: '豪装', value: DecorationStyle.DELUXE },
        ],
      },
    },
    {
      field: 'rent',
      label: '租金(元/月)',
      component: 'InputNumber',
      required: true,
      componentProps: {
        min: 0,
        placeholder: '请输入租金',
        style: { width: '100%' },
      },
    },
    {
      field: 'deposit',
      label: '押金(元)',
      component: 'InputNumber',
      required: true,
      componentProps: {
        min: 0,
        placeholder: '请输入押金',
        style: { width: '100%' },
      },
    },
    {
      field: 'status',
      label: '房态',
      component: 'Select',
      componentProps: {
        placeholder: '选择房态',
        options: [
          { label: '空置', value: RoomStatus.VACANT },
          { label: '已预订', value: RoomStatus.RESERVED },
          { label: '已出租', value: RoomStatus.RENTED },
          { label: '维修中', value: RoomStatus.MAINTENANCE },
        ],
      },
    },
    {
      field: 'rentType',
      label: '出租类型',
      component: 'Select',
      componentProps: {
        placeholder: '选择出租类型',
        options: [
          { label: '整租', value: RentType.WHOLE },
          { label: '合租', value: RentType.SHARED },
        ],
      },
    },
    {
      field: 'building',
      label: '楼栋',
      component: 'Select',
      componentProps: {
        placeholder: '选择楼栋',
        options: [
          { label: 'A栋', value: 'A栋' },
          { label: 'B栋', value: 'B栋' },
          { label: 'C栋', value: 'C栋' },
        ],
      },
    },
    {
      field: 'floor',
      label: '楼层',
      component: 'InputNumber',
      componentProps: {
        min: 1,
        placeholder: '请输入楼层',
        style: { width: '100%' },
      },
    },
    {
      field: 'facilities',
      label: '配套设施',
      component: 'Select',
      componentProps: {
        mode: 'multiple',
        placeholder: '选择配套设施',
        options: [
          { label: '空调', value: '空调' },
          { label: '热水器', value: '热水器' },
          { label: '洗衣机', value: '洗衣机' },
          { label: '冰箱', value: '冰箱' },
          { label: '电视', value: '电视' },
          { label: 'WiFi', value: 'WiFi' },
          { label: '阳台', value: '阳台' },
          { label: '厨房', value: '厨房' },
          { label: '衣柜', value: '衣柜' },
          { label: '书桌', value: '书桌' },
        ],
      },
    },
    {
      field: 'tags',
      label: '房源标签',
      component: 'Select',
      componentProps: {
        mode: 'tags',
        placeholder: '输入标签，按回车确认',
      },
    },
    {
      field: 'address',
      label: '地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入详细地址',
      },
    },
    {
      field: 'description',
      label: '房源描述',
      component: 'InputTextArea',
      componentProps: {
        rows: 4,
        placeholder: '请输入房源描述',
      },
    },
  ];
}

// 表格列配置
export function useGridColumns(): VxeTableColumnOptions[] {
  return [
    {
      field: 'roomNumber',
      title: '房间号',
      width: 100,
    },
    {
      field: 'area',
      title: '面积(㎡)',
      width: 100,
    },
    {
      field: 'orientation',
      title: '朝向',
      width: 80,
      formatter: ({ cellValue }) => {
        const options = [
          { label: '北', value: Orientation.NORTH },
          { label: '南', value: Orientation.SOUTH },
          { label: '东', value: Orientation.EAST },
          { label: '西', value: Orientation.WEST },
          { label: '东北', value: Orientation.NORTHEAST },
          { label: '西北', value: Orientation.NORTHWEST },
          { label: '东南', value: Orientation.SOUTHEAST },
          { label: '西南', value: Orientation.SOUTHWEST },
        ];
        return (
          options.find((opt) => opt.value === cellValue)?.label || cellValue
        );
      },
    },
    {
      field: 'rent',
      title: '租金(元/月)',
      width: 120,
    },
    {
      field: 'deposit',
      title: '押金(元)',
      width: 120,
    },
    {
      field: 'status',
      title: '房态',
      width: 100,
      slots: {
        default: ({ row }) => {
          const statusConfig = {
            [RoomStatus.VACANT]: { text: '空置', color: 'green' },
            [RoomStatus.RESERVED]: { text: '已预订', color: 'orange' },
            [RoomStatus.RENTED]: { text: '已出租', color: 'blue' },
            [RoomStatus.MAINTENANCE]: { text: '维修中', color: 'red' },
          };
          const config = statusConfig[row.status];
          return config
            ? `<Tag color="${config.color}">${config.text}</Tag>`
            : row.status;
        },
      },
    },
    {
      field: 'decorationStyle',
      title: '装修风格',
      width: 100,
      formatter: ({ cellValue }) => {
        const options = [
          { label: '简装', value: DecorationStyle.SIMPLE },
          { label: '中装', value: DecorationStyle.MEDIUM },
          { label: '精装', value: DecorationStyle.LUXURY },
          { label: '豪装', value: DecorationStyle.DELUXE },
        ];
        return (
          options.find((opt) => opt.value === cellValue)?.label || cellValue
        );
      },
    },
    {
      field: 'rentType',
      title: '出租类型',
      width: 100,
      formatter: ({ cellValue }) => {
        const options = [
          { label: '整租', value: RentType.WHOLE },
          { label: '合租', value: RentType.SHARED },
        ];
        return (
          options.find((opt) => opt.value === cellValue)?.label || cellValue
        );
      },
    },
    {
      field: 'building',
      title: '楼栋',
      width: 80,
    },
    {
      field: 'floor',
      title: '楼层',
      width: 80,
    },
    {
      field: 'facilities',
      title: '配套设施',
      width: 200,
      formatter: ({ cellValue }) => {
        return Array.isArray(cellValue) ? cellValue.join('、') : cellValue;
      },
    },
    {
      field: 'tags',
      title: '标签',
      width: 150,
      formatter: ({ cellValue }) => {
        return Array.isArray(cellValue) ? cellValue.join('、') : cellValue;
      },
    },
    {
      field: 'iotDevices',
      title: 'IOT设备',
      width: 120,
      slots: {
        default: ({ row }) => {
          const onlineCount =
            row.iotDevices?.filter((d) => d.status === 'online').length || 0;
          const totalCount = row.iotDevices?.length || 0;
          return `<Badge count="${onlineCount}/${totalCount}" />`;
        },
      },
    },
    {
      field: 'createdAt',
      title: '创建时间',
      width: 180,
      formatter: ({ cellValue }) => {
        return new Date(cellValue).toLocaleString();
      },
    },
  ];
}

// 搜索表单Schema
export function useGridFormSchema(): FormSchema[] {
  return [
    {
      field: 'roomNumber',
      label: '房间号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入房间号',
      },
    },
    {
      field: 'status',
      label: '房态',
      component: 'Select',
      componentProps: {
        placeholder: '选择房态',
        allowClear: true,
        options: [
          { label: '空置', value: RoomStatus.VACANT },
          { label: '已预订', value: RoomStatus.RESERVED },
          { label: '已出租', value: RoomStatus.RENTED },
          { label: '维修中', value: RoomStatus.MAINTENANCE },
        ],
      },
    },
    {
      field: 'building',
      label: '楼栋',
      component: 'Select',
      componentProps: {
        placeholder: '选择楼栋',
        allowClear: true,
        options: [
          { label: 'A栋', value: 'A栋' },
          { label: 'B栋', value: 'B栋' },
          { label: 'C栋', value: 'C栋' },
        ],
      },
    },
  ];
}
